//
//  WLPermissionTool.m
//  DongCi
//
//  Created by kenshin on 21/06/2017.
//  Copyright © 2017 wmlives. All rights reserved.
//

#import "WLPermissionTool.h"

#import <AssetsLibrary/AssetsLibrary.h>
#import <AVFoundation/AVFoundation.h>
#import <Photos/Photos.h>

@implementation WLPermissionTool

+ (BOOL)isPhotoLibraryAccessOK
{
    PHAuthorizationStatus authStatusPhoto = [PHPhotoLibrary authorizationStatus];
    return authStatusPhoto == AVAuthorizationStatusAuthorized;
}

+ (BOOL)isCameraAccessOK
{
    AVAuthorizationStatus author = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if (author == AVAuthorizationStatusDenied || author == AVAuthorizationStatusRestricted) {
        return NO;
    }
    return YES;
}

+ (void)requestCameraAccess:(TKActionSuccessBlock)success {
    [AVCaptureDevice requestAccessForMediaType:
     AVMediaTypeVideo completionHandler:^(BOOL granted) {
         if (success) {
             KSAsyncRunInMain(^{
                 success(granted);
             });
         }
     }];
}

+ (void)requestPhotoAccess:(TKActionSuccessBlock)success {
    [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
        if (success) {
            KSAsyncRunInMain(^{
                success(status == PHAuthorizationStatusAuthorized);
            });
        }
    }];
}

+ (void)requestAudioRecordAccess:(TKActionSuccessBlock)success {
    [AVCaptureDevice requestAccessForMediaType:
     AVMediaTypeAudio completionHandler:^(BOOL granted) {
         if (success) {
             KSAsyncRunInMain(^{
                 success(granted);
             });
         }
     }];
}

+ (BOOL)isAudioRecordAccessOK {
    AVAuthorizationStatus author = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];//麦克风权限
    if (author == AVAuthorizationStatusDenied || author == AVAuthorizationStatusRestricted) {
        return NO;
    }
    return YES;
    
//    [AVCaptureDevice requestAccessForMediaType:
//     AVMediaTypeAudio completionHandler:^(BOOL granted)
//     {//麦克风权限
//         if (granted) { NSLog(@"Authorized"); }
//         else{ NSLog(@"Denied or Restricted");
//         }}];
}

+ (void)showCameraAccessAlert
{
    NSString *appName = [[NSBundle mainBundle].infoDictionary valueForKey:@"CFBundleDisplayName"];
    if (!appName) appName = [[NSBundle mainBundle].infoDictionary valueForKey:@"CFBundleName"];
    NSString *des = [NSString stringWithFormat:@"是否去设置-隐私-相机中授予%@该权限？", appName];
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"无法访问相机权限" message:des preferredStyle:UIAlertControllerStyleAlert];
    
    [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];;
        if ([[UIApplication sharedApplication] canOpenURL:url]) {
            [[UIApplication sharedApplication] openURL:url];
        }

        
    }]];
    [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    [[KSNavi defaultNavi] presentViewController:alertController animated:YES completion:nil];
}

+ (void)showPhotoLibraryAccessAlert
{
    NSString *appName = [[NSBundle mainBundle].infoDictionary valueForKey:@"CFBundleDisplayName"];
    if (!appName) appName = [[NSBundle mainBundle].infoDictionary valueForKey:@"CFBundleName"];
    NSString *des = [NSString stringWithFormat:@"是否去设置-隐私-相册中授予%@该权限？", appName];
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"无法访问相册" message:des preferredStyle:UIAlertControllerStyleAlert];
    
    [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];;
        if ([[UIApplication sharedApplication] canOpenURL:url]) {
            [[UIApplication sharedApplication] openURL:url];
        }
        
        
    }]];
    [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    [[KSNavi defaultNavi] presentViewController:alertController animated:YES completion:nil];
    
}

@end
